{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f8928f5",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "\n",
    "> 1.有序的数据结构之列表\n",
    "\n",
    "> 2.用户输入\n",
    "\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3c9f76f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：如果想一个变量存放一个班同学的名字，该如何存？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bad9ae0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frences,Angelina,Taylor\n"
     ]
    }
   ],
   "source": [
    "网二_name_list =\"Frences,Angelina,Taylor\"\n",
    "print(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5e5a5c4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'An'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考：处理数据会不会比较麻烦？\n",
    "网二_name_list[8:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3eee792",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考：想知道名单中有多少个人名？\n",
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dc9174e",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db7e25e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a =[1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "35f5e5f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Frences', 'Angelina', 'Taylor'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网二_name_list =[\"Frences\",\"Angelina\",\"Taylor\"]\n",
    "print(网二_name_list,type(网二_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "887015cb",
   "metadata": {},
   "source": [
    "## 1.列表的运算法则\n",
    "列表的数据本身不做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9a7a6537",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Frences'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出网二_name_list中第一位同学的名字\n",
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d1002ac4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看网二_name_list 有多少位同学\n",
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ea3796ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查看数据是否存在\n",
    "\"Frences\"in 网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75979fd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4eb04feb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一个list，可不可以出现重复项？\n",
    "网一_age_list =[18,18,19,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3ed99fe9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 19, 19, 20]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e230ac0",
   "metadata": {},
   "source": [
    "# 2.2 关于列表的方法（function）--对列表数据进行操作\n",
    "\n",
    "> 1.增\n",
    "\n",
    "> 2.删\n",
    "\n",
    "> 3.改\n",
    "\n",
    "> 4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3805482b",
   "metadata": {},
   "outputs": [],
   "source": [
    "### append增加 增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e6a739ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Frences', 'Angelina', 'Taylor']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "05cb1091",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.append(\"666\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c56e55aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Frences', 'Angelina', 'Taylor']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "27bbe2c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3aadd673",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1a6994cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Frences', 'Angelina', 'Taylor']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "35e4d5ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李华', 'Angelina', 'Taylor', '666']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和  in  一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Frences\" in 网二_name_list:\n",
    "    网二_name_list.remove(\"Frences\")\n",
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "fcec2005",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,21]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d4505cec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'666'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a9d3dda2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李华', 'Angelina', 'Taylor']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd1d7244",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 尝试，移除19的元素\n",
    "age_list = [18,19,18,19,17,20,21]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4eab7817",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 21]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1：remove（value），先决条件，知道值出现的次数\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4e60df54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 21]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop（index），先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdecc5e2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
